//
//  ScheduleController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-20.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "ScheduleController.h"
#import "scheduleCell.h"

@interface ScheduleController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ScheduleController

- (void)dealloc
{
    [_dayArray release];
    [_dayTableView release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dayArray = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor redColor]];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setTitle:@"日程"];
   // [self createDayTableView];
    [self startAsync];
  
}


- (void)createDayTableView
{
    self.dayTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    [_dayTableView registerClass:[scheduleCell class] forCellReuseIdentifier:@"ok"];
    _dayTableView.dataSource = self;
    _dayTableView.delegate = self;
    [self.view addSubview:_dayTableView];
    [_dayTableView release];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return [_titleArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = [_titleArray objectAtIndex:section];
    
    return [arr count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    scheduleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
    
    NSMutableDictionary *dic = [_titleArray objectAtIndex:indexPath.section];
    NSMutableArray *arr = [dic objectForKey:@"array"];
    NSMutableDictionary *sDic = [arr lastObject];
    cell.nameLabel.text = [sDic objectForKey:@"name"];
    return cell;
}



- (void)startAsync
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/2388057349/schedule/?offline=false"]];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        
        for (NSMutableDictionary *dic in array) {
            NSMutableArray *placeArray = [dic objectForKey:@"places"];
            for (NSMutableDictionary *dic1 in placeArray) {
                NSMutableDictionary *dic2 = [dic1 objectForKey:@"place"];
                [self.titleArray addObject:dic2];
                
            }
            NSMutableDictionary *ssDic = [placeArray lastObject];
            NSMutableArray *sArray = [ssDic objectForKey:@"pois"];
            NSMutableDictionary *nameDic = [sArray lastObject];
//            [self.dayArray addObject:nameDic];
            
            NSMutableArray *sArr = [NSMutableArray array];
            [sArr addObject:nameDic];
            NSMutableDictionary *sssDic = [NSMutableDictionary dictionary];
            [sssDic setObject:sArr forKey:@"array"];
            [self.titleArray addObject:sssDic];
            [self.titleArray addObject:_dayArray];
        }
        
        [_dayTableView reloadData];
    }];

}





@end
